<?php
header("Content-type:text/html;charset=utf-8");
//var_dump("--------------------------初始化 start----------------------------");
$host="https://smlopenapi.esign.cn";//请求网关host
$project_id="";//应用id
$project_scert="";//密钥
\Esign\Factory\Factory::init($host,$project_id,$project_scert);
\Esign\Factory\Factory::setDebug(true);//是否开启日志记录，传true或false,日志存放在根目录的phplog.txt文件
\Esign\Factory\Factory::setDebugPath(__DIR__);

$file_template = \Esign\Factory\Base\FileTemplate::createFileByTemplate();
$file_template_resp = $file_template->execute()->getBody();
$file_id = $file_template_resp->data->fileId;//文件id保存好，后续使用


$createFlowOneStep = new \Esign\Factory\Signfile\CreateFlowOneStep();
$doc = new \Esign\Factory\Bean\Doc();
$doc->setFileId($fileId);
$doc->setFileName("合同名.pdf");
$createFlowOneStep->setDoc([$doc]);

$flowInfo = new \Esign\Factory\Bean\FlowInfo();
$flowInfo->setBusinessScene("b2c合同签署测试");
$flowInfo->setAutoArchive(true);//自动归档
$flowInfo->setAutoInitiate(true);//自动开启流程
$flowInfo->setSignValidity();
$flowInfo->setContractValidity();
$flowInfo->setInitiatorAccountId();
$flowInfo->setInitiatorAuthorizedAccountId();
$flowInfo->setFlowConfigInfo([
    "noticeDeveloperUrl"  =>  $eSign->noticeDeveloperUrl,
    "noticeType"    =>  1,
    "signPlatform"  =>  "1,2"
]);
$createFlowOneStep->setFlowInfo($flowInfo);

//企业签署方信息开始
$signer_company = new \Esign\Factory\Bean\Signer();
$signer_company->setPlatformSign(true);
$signer_company->setSignOrder(1);
$signer_company->setThirdOrderNo("c");
$company_sign_field = new \Esign\Factory\Bean\Signfield();
$company_sign_field->setAutoExecute(true);//自动签署
$company_sign_field->setActorIndentityType(2);
$company_sign_field->setFileId();
$company_sign_field->setSealId();
$company_sign_field->setSignType(1);
$posBean = new \Esign\Factory\Bean\PosBean();
$company_sign_field->setPosBean($posBean->setPosPage(5)->setPosX(180)->setPosY(300));
$company_sign_field->setWidth(100);
$company_sign_field->setSignDateBeanType(0);
$signer_company->setSignfields([$company_sign_field]);
//企业签署方信息结束
//[
//    "platformSign"  =>  false,
//    "signOrder"     =>  1,
//    "signerAccount" =>  [
//        "signerAccountId"   =>    $customer_account_id
//    ],
//    "signfields"    =>  [
//        [
//            "autoExecute"   =>  false,//是否自动执行签署，默认falsetrue-自动签署，false-手动签署。
//            "fileId"         =>  $fileId,
//            "signType"       =>  1,//签署类型，0-不限，1-单页签署，2-骑缝签署，默认1
//            "posBean"        =>  [
//                'posPage' => 5,
//                'posX' => 236,
//                'posY' => 546,
//            ],
//            "width"  =>  80,
//            "signDateBeanType"   =>  0//是否需要添加签署日期，0-禁止 1-必须 2-不限制，默认0
//        ]
//    ],
//    "thirdOrderNo"  =>  "a"//第三方流水号
//],
//甲方(客户)签署信息开始
$signer_customer = new \Esign\Factory\Bean\Signer();
$signer_customer->setPlatformSign(true);
$signer_customer->setSignOrder(1);
$customerSignerAccount = new \Esign\Factory\Bean\SignerAccount();
$customerSignerAccount->setSignerAccountId($customerAccountId);
$signer_customer->setSignerAccount($customerSignerAccount);
$signer_customer->setThirdOrderNo("a");
$customer_sign_field = new \Esign\Factory\Bean\Signfield();
$customer_sign_field->setAutoExecute(false);//自动签署
$customer_sign_field->setFileId();
$customer_sign_field->setSignType(1);
$posBean = new \Esign\Factory\Bean\PosBean();
$customer_sign_field->setPosBean($posBean->setPosPage(5)->setPosX(236)->setPosY(546));
$customer_sign_field->setWidth(80);
$customer_sign_field->setSignDateBeanType(0);
$signer_customer->setSignfields([$customer_sign_field]);
//甲方(客户)签署信息结束

//乙方(阿姨)签署信息开始
$signer_worker = new \Esign\Factory\Bean\Signer();
$signer_worker->setPlatformSign(true);
$signer_worker->setSignOrder(1);
$workerSignerAccount = new \Esign\Factory\Bean\SignerAccount();
$workerSignerAccount->setSignerAccountId($workerAccountId);
$signer_worker->setSignerAccount($workerSignerAccount);
$signer_worker->setThirdOrderNo("b");
$worker_sign_field = new \Esign\Factory\Bean\Signfield();
$worker_sign_field->setAutoExecute(false);//自动签署
$worker_sign_field->setFileId();
$worker_sign_field->setSignType(1);
$posBean = new \Esign\Factory\Bean\PosBean();
$worker_sign_field->setPosBean($posBean->setPosPage(5)->setPosX(155)->setPosY(452));
$worker_sign_field->setWidth(80);
$worker_sign_field->setSignDateBeanType(0);
$signer_worker->setSignfields([$worker_sign_field]);
//乙方(阿姨)签署信息结束
$createFlowOneStep->setSigners([$signer_company,$signer_customer,$signer_worker]);

$attachment = new \Esign\Factory\Bean\Attachment();
$attachment->setFileId("123");
$attachment->getAttachmentName("保险.pdf");
$createFlowOneStep->setAttachments([$Attachment]);
$copiers = new \Esign\Factory\Bean\Copier();
$copiers->setCopierAccountId();
$copiers->setCopierIdentityAccountType(0);
$createFlowOneStep->setCopiers([$copiers]);
$flowOneStepResp = $createFlowOneStep->execute();
$flowOneStepJson = json_decode($flowOneStepResp->getBody());
$flowId = $flowOneStepJson->data->flowId;//流程id保存好
var_dump("------------------ 一步发起签署 end -----------------");

var_dump("------------------ 获取签署地址 start -----------------");
$getFileSignUrl = \Esign\Factory\Base\SignFile::getFileSignUrl($flowId, $accountId);
$getFileSignUrl->setOrganizeId($orgId);
$getFileSignUrlResp = $getFileSignUrl->execute();
$getFileSignUrlJson = json_decode($getFileSignUrlResp->getBody());
$shortUrl = $getFileSignUrlJson->data->shortUrl;
var_dump("签署短连接,复制到浏览器打开\n".$shortUrl);
var_dump("------------------ 获取签署地址 end -----------------");


